<template>
  <div class="app-container">
    <el-radio-group v-model="type" style="margin-bottom: 20px;" @change="getList(true)">
      <el-radio-button label="toRevise">待整改记录</el-radio-button>
      <el-radio-button label="toReview">待复查记录</el-radio-button>
      <el-radio-button label="postpone">延期待审</el-radio-button>
      <el-radio-button label="finished">与我相关</el-radio-button>
      <el-radio-button label="selfTest">项目自检</el-radio-button>
      <el-radio-button label="examine">公司检查</el-radio-button>
      <el-radio-button label="TasksToDo">计划任务</el-radio-button>
    </el-radio-group>
    <div v-if="type == 'selfTest'">
      <el-radio-group v-model="typeData" style="margin-bottom: 20px;" @change="getListData(true)">
        <el-radio-button label="typeData1">项目自检待办</el-radio-button>
        <el-radio-button label="typeData2">项目自检历史</el-radio-button>
      </el-radio-group>
    </div>
    <div v-if="type == 'examine'">
      <el-radio-group v-model="typeData" style="margin-bottom: 20px;" @change="getListData(true)">
        <el-radio-button label="typeData1">公司检查待办</el-radio-button>
        <el-radio-button label="typeData2">公司检查历史</el-radio-button>
      </el-radio-group>
    </div>
    <div v-if="type == 'TasksToDo'">
      <el-radio-group v-model="typeData" style="margin-bottom: 20px;" @change="getListData(true)">
        <el-radio-button label="typeData1">计划任务待办</el-radio-button>
        <el-radio-button label="typeData2">计划任务历史</el-radio-button>
      </el-radio-group>
    </div>
    <el-table v-loading="loading" :data="list" key="index">
      <span v-if="type != 'selfTest' && type != 'examine' && type != 'TasksToDo'" key="1">
        <el-table-column label="问题编号" width="200" prop="workNo" />
        <el-table-column label="问题整改状态" width="120" prop="dataStatus">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.list_quality_data_status" :value="scope.row.dataStatus" />
          </template>
        </el-table-column>
        <el-table-column label="问题描述" width="200" prop="questionDesc" />
      </span>

      <!-- 项目自检 -->
      <span v-if="type == 'selfTest'" key="2">
        <el-table-column label="任务名称" prop="checkTaskName" width="320"/>
        <el-table-column label="检查类型" prop="checkTypeName" />
        <el-table-column label="任务状态" prop="checkStatusName">
          <template slot-scope="scope">
            <el-tag effect="dark" :color="scope.row.status == '1'
              ? rgb1
              : scope.row.status == '2'
                ? rgb2
                : scope.row.status == '3'
                  ? rgb3
                  : rgb4
              " style="border: 0">
              <dict-tag :options="dict.type.check_status" :value="scope.row.status" />
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="创建人" prop="createBy" />
        <el-table-column label="质量问题总数" prop="hiddenDangerSum" align="center">
          <template slot-scope="scope">
            <el-link type="primary" style="text-decoration: underline;" underline @click="showQuailityList(scope.row)">{{
              scope.row.hiddenDangerSum
            }}</el-link>
          </template>
        </el-table-column>
        <el-table-column label="质量问题整改情况" prop="hiddenAbarbeitung" align="center" />
        <el-table-column label="计划检查时间" prop="planCheckTime" width="220px"/>
        <el-table-column label="任务下发时间" prop="createTime"  width="160px"/>

      </span>
      <!-- 计划任务 -->
      <span v-if="type == 'TasksToDo'" key="3">
        <el-table-column label="计划名称" prop="title" />
        <el-table-column label="计划内容" prop="remark" />
        <!-- <el-table-column label="有效时间" prop="dateRange">
          <template slot-scope="scope">
          <span v-if="scope.row.dateRange != null">
            <span>{{ scope.row.dateRange[0] }}</span>
            <span>至</span>
            <span>{{ scope.row.dateRange[1] }}</span>
          </span>
          <span v-else style="color: red">未设置有效时间</span>
        </template>
        </el-table-column> -->
        <el-table-column label="检查频次" align="center" prop="excludeFrequency">
          <template slot-scope="scope">
            <span v-if="scope.row.excludeFrequency != null">
              {{ scope.row.excludeFrequency }}次/{{ scope.row.dateTypeName }}
            </span>
            <span v-else style="color: red">未设置排查频次</span>
          </template>
        </el-table-column>
        <el-table-column label="排查类别" align="center" prop="excludeType">
          <template slot-scope="scope">
            <div>{{ getString(scope.row.excludeType) }}</div>
          </template>
        </el-table-column>
        <el-table-column label="有效时间" align="center" width="200" prop="startDate">
          <template slot-scope="scope">
            <div>{{ scope.row.startDate }}至{{ scope.row.endDate }}</div>
          </template>
        </el-table-column>
        <el-table-column label="任务下发时间" align="center" prop="createTime">
          <!-- <template slot-scope="scope">
            <div>{{ parseTime(scope.row.createTime, '{y}{m}{d}{h}{i}{s}') }}</div>
          </template> -->
        </el-table-column>
        <!-- <el-table-column label="质量问题总数" prop="qualityNum" align="center" /> -->
        <el-table-column label="质量问题总数" prop="qualityNum" align="center">
          <template slot-scope="scope">
            <el-link type="primary" style="text-decoration: underline;" underline @click="showQuailityList1(scope.row)">{{
              scope.row.qualityNum
            }}</el-link>
          </template>
        </el-table-column>
        <el-table-column label="质量问题整改情况" prop="qualityOverNum" align="center" />
      </span>

      <span v-if="type == 'examine'" key="4">
        <el-table-column label="任务名称" prop="checkName" />
        <el-table-column label="检查类型" prop="checkType" />
        <!-- <el-table-column label="检查类型名称"  prop="checkTypeName" /> -->
        <el-table-column label="组织名称" prop="deptName" />
        <el-table-column label="检查状态" prop="status">
          <template slot-scope="scope">
            <el-tag effect="dark" :color="scope.row.status == '1'
              ? rgb1
              : scope.row.status == '2'
                ? rgb2
                : scope.row.status == '3'
                  ? rgb3
                  : rgb4
              " style="border: 0">
              <dict-tag :options="dict.type.check_status" :value="scope.row.status" />
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="计划开始检查时间" prop="planStartTime" />
        <el-table-column label="计划检查结束时间" prop="planEndTime" />
        <el-table-column label="任务下发时间" prop="createTime" width="160"/>
        <el-table-column label="检查依据" prop="checkBasis">
          <template slot-scope="scope">
            {{ scope.row.checkBasis == '1' ? '自由检查' : '其他' }}
          </template>
        </el-table-column>
        <!-- <el-table-column label="质量问题总数" prop="qualityNum" align="center" />
         -->
        <el-table-column label="质量问题总数" prop="qualityNum" align="center">
          <template slot-scope="scope">
            <el-link type="primary" style="text-decoration: underline;" underline @click="showQuailityList1(scope.row)">{{
              scope.row.qualityNum
            }}</el-link>
          </template>
        </el-table-column>
        <el-table-column label="质量问题整改情况" prop="qualityOverNum" align="center" />
      </span>

      <span v-if="type != 'selfTest' && type != 'examine' && type != 'TasksToDo'" key="5">
        <el-table-column label="整改人" width="140" prop="revisePeopleName" />
        <el-table-column label="复查人" width="140" prop="reviewUserName" />
        <el-table-column label="通知人" width="120" prop="notifierName" />
        <!-- <el-table-column label="发文单位类型" prop="fileType">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.list_quality_file_type" :value="scope.row.fileType" />
          </template>
        </el-table-column> -->
        <el-table-column label="质量问题来源" width="140" prop="dataSource">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.list_quality_data_source" :value="scope.row.dataSource" />
          </template>
        </el-table-column>
        <el-table-column label="要求整改完成日期" width="140" prop="reviseTheDates" />
        <!-- <el-table-column v-if="type != 'postpone'" label="问题描述" prop="questionDesc" >
        <template slot-scope="scope">
          <el-tooltip class="item" effect="light" style="width:100px;" popper-class="tooltip-wrap" :content="scope.row.questionDesc">
          <div style="white-space: nowrap;overflow: hidden;text-overflow: ellipsis;">{{ scope.row.questionDesc }}</div>
          </el-tooltip>
        </template>
      </el-table-column> -->

        <el-table-column v-if="type == 'postpone'" label="延期原因" prop="questionDesc" />
      </span>
      <el-table-column fixed="right" label="操作" align="center" width="300" v-if="typeData != 'typeData2'">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleDelayData(scope.row)"
            v-if="type == 'postpone'">审批</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleSelfTest(scope.row)"
            v-if="type == 'selfTest'">上报质量问题</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleSelfTest(scope.row)"
            v-if="type == 'TasksToDo'">上报质量问题</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="endTask(scope.row)"
            v-if="type == 'selfTest'">结束任务</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleView(scope.row)"
            v-if="type != 'examine' && type != 'selfTest' && type != 'TasksToDo'">查看</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleSelfTest(scope.row)"
            v-if="type == 'examine'">上报质量问题</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="finishState(scope.row)"
            v-if="type == 'examine'">结束任务</el-button>

          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleRevise(scope.row)"
            v-if="type == 'toRevise'">整改</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleReview(scope.row)"
            v-if="type == 'toReview'">复查</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit"
            v-if="type != 'toReview' && type != 'finished' && type != 'postpone' && type != 'selfTest' && type != 'examine' && type != 'TasksToDo'"
            @click="handleOver(scope.row)">移交</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete"
            v-if="scope.row.extensionNum == '0' && type == 'toRevise'" @click="handleDelay(scope.row)">申请延期</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList(true)" />
    <ViewDialog :visible.sync="viewDialogVisible" v-if="viewDialogVisible" :data="selectRow" :title="title"
      :operateType="operateType" :dicts="dict.type" />
    <ReviseDialog :visible.sync="reviseDialogVisible" v-if="reviseDialogVisible" :data="selectRow" :title="title"
      :operateType="operateType" @refresh="getList(true)" />
    <ReviewDialog :visible.sync="reviewDialogVisible" v-if="reviewDialogVisible" :data="selectRow" :title="title"
      :operateType="operateType" @refresh="getList(true)" />
    <OverDialog :visible.sync="overDialogVisible" v-if="overDialogVisible" :data="selectRow" :title="title"
      :operateType="operateType" @refresh="getList(true)" />
    <DelayDialog :visible.sync="delayDialogVisible" v-if="delayDialogVisible" :data="selectRow" :title="title"
      :operateType="operateType" @refresh="getList(true)" />
    <QualityListDialog :visible.sync="qualityListDialogVisible" v-if="qualityListDialogVisible" :data="selectRow" />
    <el-dialog :title="title" :visible.sync="dialogVisible" width="50%">
      <el-divider content-position="left">任务信息</el-divider>
      <el-form ref="form" :model="formRow" label-width="140px" :inline="true">
        <el-form-item label="任务名称:">
          {{ formRow.checkTaskName || formRow.checkName || formRow.title }}
        </el-form-item>
        <el-form-item label="检查类型:">
          {{ formRow.checkTypeName || formRow.checkType ||formRow.dateTypeName }}
        </el-form-item>
        <el-form-item label="任务下发时间:" v-if="formRow.createTime">
          {{formRow.createTime}}
        </el-form-item>
        <el-form-item label="组织名称:" v-if="type == 'examine'">
          {{ formRow.deptName }}
        </el-form-item>
        <el-form-item label="检查状态:" v-if="formRow.status">
          {{ formRow.status == '1' ? '任务已下发' : formRow.status == '2' ? '检查中' : formRow.status == '3' ? '任务已结束' : '已取消' }}
        </el-form-item>
        <span v-if="type != 'examine'">
          <el-form-item label="任务状态:" v-if="formRow.checkStatusName">
          {{ formRow.checkStatusName }}
        </el-form-item>
          <el-form-item label="创建人:" v-if="formRow.createBy">
            {{ formRow.createBy }}
          </el-form-item>
          <el-form-item label="质量问题总数:" v-if="formRow.hiddenDangerSum" >
            {{ formRow.hiddenDangerSum }}
          </el-form-item>
          <el-form-item label="计划检查时间:" v-if="formRow.planCheckTime">
            {{ formRow.planCheckTime }}
          </el-form-item>
        </span>
        <span v-else>
          <el-form-item label="计划开始检查时间:" v-if="formRow.planStartTime">
            {{ formRow.planStartTime }}
          </el-form-item>
          <el-form-item label="计划检查结束时间:" v-if="formRow.planEndTime">
            {{ formRow.planEndTime }}
          </el-form-item>
          <el-form-item label="检查依据:" v-if="formRow.checkBasis">
            {{ formRow.checkBasis == '1' ? '自由检查' : '其他' }}
          </el-form-item>
        </span>
      </el-form>
      <!-- <el-divider content-position="left">质量问题信息</el-divider> -->
      <el-form v-loading="loading" :model="item" label-width="100px" v-for="(item, index) in formRowList" :key="index">
        <el-divider content-position="left">质量问题信息{{ index + 1 }}</el-divider>
        <el-row>
          <el-col :span="24">
            <el-form-item label="质量问题库" prop="libId">
              <!-- <el-input type="text" v-model="item.libId"/> -->
              <!-- <el-tree :data="libOptions" :props="defaultProps" @node-click="handleNodeClick" ></el-tree> -->
              <treeselect :disable-branch-nodes="true" :normalizer="normalizer" v-model="item.libId" @select="libChange" row-key="id" :flat="true"
                @input="libChangeIndex(index)" :options="libOptions" :default-expand-level="100" placeholder="质量问题库" />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="质量问题名称" prop="fileId">
              <el-input type="text" v-model="item.fileId" />
            </el-form-item>
          </el-col> -->
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="问题编号" prop="workNo">
              <el-input type="text" v-model="item.workNo" readonly />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="问题描述" prop="planCheckTime">
              <el-input type="textarea" :rows="2" v-model="item.questionDesc" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="补充说明" prop="supplement">
              <el-input type="textarea" :rows="2" v-model="item.supplement" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="防治措施" prop="controlMeasures">
              <el-input type="textarea" :rows="2" v-model="item.controlMeasures" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="责任区域" prop="dutyAreaId">
              <!-- <el-input type="text" v-model="item.dutyAreaId"/> -->
              <treeselect @input="libChangeData" @select="changeDutyAreaId(index)" :normalizer="normalizer2" v-model="item.dutyAreaId"
                :options="dutyAreaOptions" :default-expand-level="100" placeholder="请选择责任区域" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="检查类型" prop="checkType">
              <!-- <el-input type="text" v-model="item.checkType"/> -->
              <el-select v-model="item.checkType" style="width:356px">
                <el-option v-for="dict in checkTypeOptions" :key="dict.id" :label="dict.label" :value="dict.id" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="问题等级" prop="questionLevel">
              <el-select v-model="item.questionLevel" style="width:356px">
                <el-option v-for="dict in dict.type.list_quality_level" :key="dict.value" :label="dict.label"
                  :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否返工" prop="reworkOrNot">
              <el-select v-model="item.reworkOrNot" style="width:356px">
                <el-option v-for="dict in dict.type.list_quality_rework_or_not" :key="dict.value" :label="dict.label"
                  :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <!-- <el-col :span="12">
            <el-form-item label="是否公开" prop="publishOrNot">
              <el-select v-model="item.publishOrNot" style="width:356px" placeholder="请选择是否公开">
                <el-option v-for="dict in dict.type.list_quality_publish_or_not" :key="dict.value" :label="dict.label"
                  :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item label="整改期限" prop="rectificationDeadlineStatus">
              <el-select v-model="item.rectificationDeadlineStatus" style="width:356px" placeholder="请选择整改期限">
                <el-option v-for="dict in dict.type.list_quality_rectification_deadline_status" :key="dict.value"
                  :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="问题类型" prop="questionBeyond">
              <el-select v-model="item.questionBeyond" style="width:356px" placeholder="请选择问题类型">
                <el-option v-for="dict in dict.type.list_quality_question_beyond" :key="dict.value" :label="dict.label"
                  :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="质量问题分类" prop="questionId">
              <!-- <el-input type="text" v-model="item.questionId"/> -->
              <treeselect :disable-branch-nodes="true" :normalizer="normalizer2" v-model="item.questionId"
                :options="libOptions" :default-expand-level="100" placeholder="质量问题库" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="原因分析" prop="questionAnalysis">
              <el-select v-model="item.questionAnalysis" style="width:356px" placeholder="请选择原因分析">
                <el-option v-for="dict in dict.type.list_quality_question_analysis" :key="dict.value" :label="dict.label"
                  :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="数据来源" prop="dataSource">
              <el-select v-model="item.dataSource" disabled style="width:356px" placeholder="请选择问题分析">
                <el-option v-for="dict in dict.type.list_quality_data_source" :key="dict.value" :label="dict.label"
                  :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="整改截止时间" prop="reviseTheDates">
              <el-date-picker v-model="item.reviseTheDates" style="width:356px" type="date" placeholder="请选择整改截止时间"
                value-format="yyyy-MM-dd">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="复查人" prop="reviewUserNameId">
              <el-select v-model="item.reviewUserNameId" style="width:356px" placeholder="请选择复查人">
                <el-option v-for="dict in peopleOptions" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="整改人" prop="revisePeopleId">
              <el-select v-model="item.revisePeopleId" style="width:356px" placeholder="请选择检查人">
                <el-option v-for="dict in peopleOptions" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="通知人" prop="notifierId">
              <el-select v-model="item.notifierId" style="width:356px" placeholder="请选择通知人">
                <el-option v-for="dict in peopleOptions" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col>
            <el-form-item label="现场照片" prop="fileIds">
              <SimpleImageUpload v-model="item.fileIds"></SimpleImageUpload>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col align="center">
            <el-button size="mini" type="danger" @click="delRowData(index)" v-if="index > 0">删除</el-button>
          </el-col>
        </el-row>
      </el-form>
      <el-button size="mini" type="primary" @click="formRowData()">{{ formRowList.length == 0 ?
        '添加质量问题' : "再次添加质量问题" }}</el-button>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogSplitData">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { getRegion } from "@/api/biz/duty/region";
import refreshMixin from "@/common/refreshMixin"
import ViewDialog from "../qualityFileItem/components/addDialog.vue"
import ReviseDialog from "./components/reviseDialog.vue"
import ReviewDialog from "./components/reviewDialog.vue"
import OverDialog from "./components/overDialog.vue"
import DelayDialog from "./components/delayDialog.vue"
import QualityListDialog from "./components/qualityListDialog.vue"
import SimpleImageUpload from "@/components/SimpleImageUpload/index";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { getRevisePeople, getDutyArea } from "@/api/biz/ledger/ledger";
import { getTree } from "@/api/biz/enter/enterNext";
import { getDataList, getPostpone, getSelfTest, endTask, getExamine, finishTask, getTasksToDo, splitData, getQuestionList, addSelfTest } from "./api"
export default {
  name: "MyTask",
  components: { ViewDialog, ReviseDialog, ReviewDialog, OverDialog, DelayDialog, Treeselect, QualityListDialog,SimpleImageUpload },
  mixins: [refreshMixin],
  dicts: ["list_quality_data_source", "list_quality_data_status", "list_quality_rectification_deadline_status",
    "list_quality_question_beyond", "list_quality_question_analysis", "list_quality_level",
    "list_quality_rework_or_not", "list_quality_publish_or_not", "list_quality_file_type", "list_quality_level", "biz_exclude_type", "check_status"],
  data() {
    return {
      rgb1: "rgb(147, 210, 243)",
      rgb2: "rgb(255, 191, 107)",
      rgb3: "rgb(162, 239, 77)",
      rgb4: "rgb(144, 147, 153)",
      libOptions: [],
      type: "toRevise",
      typeData: "typeData1",
      loading: false,
      list: [],
      total: 0,
      peopleOptions: null,
      formRowList: [{
        libId: null,
        fileId: null,
        workNo: null,
        planCheckTime: null,
        supplement: null,
        controlMeasures: null,
        dutyAreaId: null,
        checkType: null,
        questionLevel: null,
        reworkOrNot: null,
        publishOrNot: null,
        rectificationDeadlineStatus: null,
        questionBeyond: null,
        questionAnalysis: null,
        questionId: null,
        reviseTheDates: null,
        dataSource: null,
        revisePeopleId: null,
        reviewUserNameId: null,
        notifierId: null,
        fileIds:null,
      }],
      formRow: {
        checkTaskName: null,
        checkTypeName: null,
        checkStatusName: null,
        createBy: null,
        hiddenDangerSum: null,
        planCheckTime: null,

      },
      queryParams: {
        pageNum: 0,
        pageSize: 10,
      },
      selectRow: {},
      operateType: 'view',
      title: "",
      dialogVisible: false,
      viewDialogVisible: false,
      reviseDialogVisible: false,
      reviewDialogVisible: false,
      overDialogVisible: false,
      delayDialogVisible: false,
      qualityListDialogVisible: false,
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      questionChildren: null,
      dutyAreaOptions: [],
      checkTypeOptions: [],
      dataIndex: null,
    }
  },
  watch: {
    $route: {
      handler(value) {
        const query = value.query
        if (query && query.type) {
          this.type = query.type
          this.queryParams.pageNum = 0
          if (this.type == "toRevise" || this.type == "toReview" || this.type == "postpone" || this.type == "finished") {
            this.getList(true)
          } else {
            this.getListData(true)
          }

        }
      },
      deep: true,
      immediate: true,
    },
  },
  methods: {
    getString(string) {
      if (!string || !string.length) {
        return ""
      }
      const buffer = string.split(",").map(item => {
        const res = this.dict.type.biz_exclude_type.find(sItem => sItem.value == item)
        return res.label
      })
      return buffer.join(" ")
    },
    showQuailityList(row) {
      if (row.hiddenDangerSum == 0) {
        return;
      }
      this.selectRow = row
      this.qualityListDialogVisible = true;
    },
    showQuailityList1(row) {
      if (row.qualityNum == 0) {
        return;
      }
      this.selectRow = row
      this.qualityListDialogVisible = true;
    },
    async getList(flag) { // true时为页内刷新，不传为project更改刷新
      this.loading = true;
      this.typeData = 'typeData1'
      const dataStatusObj = {
        "toRevise": '0',
        "toReview": '1',
        // "postpone": '1',
        "finished": '99',
      }
      const params = { ...this.queryParams, dataStatus: dataStatusObj[this.type], projectId: this.projectId }
      if (this.type == "postpone") {
        const res = await getPostpone(params)
        this.list = res.rows
        this.total = res.total
      } else if (this.type == "selfTest") {
        this.getListData(true)
      } else if (this.type == 'examine') {
        this.getListData(true)
      } else if (this.type == 'TasksToDo') {
        this.getListData(true)
      } else {
        const res = await getDataList(params)
        this.list = res.rows
        this.total = res.total
      }
      this.loading = false
    },
    async getListData(flag) {
      this.loading = true;
      const params = { ...this.queryParams, projectId: this.projectId }
      if (this.typeData == "typeData1" && this.type == "selfTest") {
        params.selectType = 0
        const res = await getSelfTest(params)
        this.list = res.data.list
        this.total = res.data.total
      } else if (this.typeData == "typeData1" && this.type == "examine") {
        params.selectType = 0
        const res = await getExamine(params)
        this.list = res.data.list
        this.total = res.data.total
      } else if (this.typeData == "typeData1" && this.type == "TasksToDo") {
        params.selectType = 0
        const res = await getTasksToDo(params)
        this.list = res.rows
        this.total = res.total
      } else if (this.typeData == "typeData2" && this.type == "selfTest") {
        params.searchType = 1
        const res = await getSelfTest(params)
        this.list = res.data.list
        this.total = res.data.total
      } else if (this.typeData == "typeData2" && this.type == "examine") {
        params.selectType = 1
        const res = await getExamine(params)
        this.list = res.data.list
        this.total = res.data.total
      } else if (this.typeData == "typeData2" && this.type == "TasksToDo") {
        params.selectType = 1
        const res = await getTasksToDo(params)
        this.list = res.rows
        this.total = res.total
      }
      this.loading = false
    },
    handleView(row) {
      this.operateType = 'view'
      this.selectRow = { ...row }
      this.title = "任务详情";
      this.viewDialogVisible = true;
    },
    handleRevise(row) {
      this.operateType = 'revise'
      this.selectRow = { ...row }
      this.title = "任务整改";
      this.reviseDialogVisible = true;
    },
    handleReview(row) {
      this.operateType = 'review'
      this.selectRow = { ...row }
      this.title = "任务复查";
      this.reviewDialogVisible = true;
    },
    handleOver(row) {
      this.operateType = 'over'
      this.selectRow = { ...row }
      this.title = "任务移交";
      this.overDialogVisible = true;
    },
    handleDelay(row) {
      this.operateType = 'delay'
      this.selectRow = { ...row }
      this.title = "任务延期申请";
      this.delayDialogVisible = true;
    },
    handleDelayData(row) {
      this.operateType = "reply"
      this.selectRow = row
      this.title = "延期申请审批";
      this.delayDialogVisible = true;
    },
    //结束任务
    async endTask(row) {
      let data = {
        id: row.id,
        status: 3,
        projectId: this.projectId
      }
      const res = await endTask(data)
      if (res.code == 200) {
        this.$message({
          type: "success",
          message: '任务结束成功'
        })
        this.getList(true)
      }
    },
    //完成
    async finishState(row) {
      let data = {
        id: row.enterCheckProjectId,
        status: 3,
        projectId: this.projectId
      }
      const res = await finishTask(data)
      if (res.code == 200) {
        this.$message({
          type: "success",
          message: '任务结束成功'
        })
        this.getList(true)
      }
    },
    //上报质量问题
    handleSelfTest(row) {
      this.formRow = row;
      this.formRowList = [{
        libId: null,
        fileId: null,
        workNo: "zlwtwj_" + this.parseTime(new Date(), '{y}{m}{d}{h}{i}{s}') + '',
        planCheckTime: null,
        supplement: null,
        controlMeasures: null,
        dutyAreaId: null,
        checkType: null,
        questionLevel: null,
        reworkOrNot: null,
        publishOrNot: null,
        rectificationDeadlineStatus: null,
        questionBeyond: null,
        questionAnalysis: null,
        questionId: null,
        reviseTheDates: null,
        dataSource: null,
        revisePeopleId: null,
        reviewUserNameId: null,
        notifierId: null,
        fileIds:null,
      }]
      if (this.type == 'selfTest') {
        this.formRowList[0].dataSource = '5'
      } else if (this.type == 'examine') {
        this.formRowList[0].dataSource = '6'
      } else if (this.type == 'TasksToDo') {
        this.formRowList[0].dataSource = '8'
      }
      this.title = "上报质量问题";
      getQuestionList().then((res) => {
        this.libOptions = res.data;
      });
      // 责任区域
      getDutyArea({ projectId: this.projectId, hiddenTrouble: '0' }).then((response) => {
        this.dutyAreaOptions = response.data;
      });
      // 检查类型
      getTree({ checkTypeNo: null }).then((response) => {
        this.checkTypeOptions = response.data
      });
      getRevisePeople({ projectId: this.projectId, hiddenTrouble: '0' }).then((data) => {
        const res = data.rows.map(item => ({ label: item.nickName, value: item.userId.toString() }))
        this.peopleOptions = res;
      });
      this.dialogVisible = true;
    },
    /** 转换责任区域数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.label || node.name || node.questionInfo,
        children: node.children || node.questionChildren,
        isDisabled: node.dataSource == null ? true : false
      };
    },
    normalizer2(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.label|| node.name ,
        children: node.children ,
      };
    },
    libChange(data) {
        this.questionChildren = data
    },
    libChangeIndex(data) {
      if (this.questionChildren != null) {
        this.formRowList[data].questionDesc = this.questionChildren.questionInfo;
        this.formRowList[data].controlMeasures = this.questionChildren.preventiveMeasures
        this.formRowList[data].questionLevel = this.questionChildren.questionLevel
        this.formRowList[data].questionId = this.questionChildren.typeNo
      }
    },
    //追加
    formRowData() {
      this.questionChildren = null
      this.loading = true
      let data = {
        libId: null,
        fileId: null,
        workNo: "zlwtwj_" + this.parseTime(new Date(), '{y}{m}{d}{h}{i}{s}') + '',
        planCheckTime: null,
        supplement: null,
        controlMeasures: null,
        dutyAreaId: null,
        checkType: null,
        questionLevel: null,
        reworkOrNot: null,
        publishOrNot: null,
        rectificationDeadlineStatus: null,
        questionBeyond: null,
        questionAnalysis: null,
        questionId: null,
        reviseTheDates: null,
        dataSource: null,
        revisePeopleId: null,
        reviewUserNameId: null,
        notifierId: null,
        fileIds:null,
      }
      if (this.type == 'selfTest') {
        data.dataSource = '5'
      } else if (this.type == 'examine') {
        data.dataSource = '6'
      } else if (this.type == 'TasksToDo') {
        data.dataSource = '8'
      }
      this.formRowList.push(data)
      this.loading = false
    },
    //删除
    delRowData(row) {
      this.loading = true
      this.formRowList.splice(row, 1);
      this.loading = false
    },
    dialogSplitData() {
      this.formRowList.forEach(item => {
        if (this.formRow.enterCheckProjectId) {
          item.fileId = this.formRow.enterCheckProjectId
        } else {
          item.fileId = this.formRow.id
        }
        item.projectId = this.projectId
      })
      if (this.type == 'selfTest') {
        addSelfTest(this.formRowList).then(data => {
          if (data.code == 200) {
            this.$modal.msgSuccess("上报成功");
            this.getList(true);
          }
        })
      } else {
        splitData(this.formRowList).then(data => {
          if (data.code == 200) {
            this.$modal.msgSuccess("上报成功");
            this.getList(true);
          }
        })
      }
      this.dialogVisible = false

    },
    libChangeData(index){
      this.dataIndex = index;
    },
    async changeDutyAreaId(dutyAreaId) {
      const res = await getRegion(dutyAreaId)
      if (res.code == 200) {
        if (res.data.correctorUserId !=null) {
          this.formRowList[this.dataIndex].revisePeopleId = res.data.correctorUserId.toString();
        }
        if (res.data.reviewerUserId!=null) {
          this.formRowList[this.dataIndex].reviewUserNameId = res.data.reviewerUserId.toString();
        }
        if(res.data.notifierUserId!=null){
          this.formRowList[this.dataIndex].notifierId = res.data.notifierUserId.toString();
        }
      }
    },
  },
}
</script>
<style lang="scss" scoped>
.tooltip-wrap .el-tooltip__popper {
  white-space: normal;
  width: 60%;
}
</style>